#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class StaffTrainDetailProvider : CY.GFive.Core.Providers.DALProvider.IStaffTrainDetailProvider
	{
		#region Sql
		private static readonly string SqlSelectStaffTrainDetail = "SELECT [Id], [TrainCode],[StaffCode],[ScoreInTheory],[ScoreInPratise] FROM [StaffTrainDetail] WHERE  [Id]=@Id";

		private static readonly string SqlInsertStaffTrainDetail = "INSERT INTO [StaffTrainDetail]([TrainCode],[StaffCode],[ScoreInTheory],[ScoreInPratise]) values(@TrainCode,@StaffCode,@ScoreInTheory,@ScoreInPratise);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateStaffTrainDetail = "UPDATE [StaffTrainDetail] SET [TrainCode] = @TrainCode,[StaffCode] = @StaffCode,[ScoreInTheory] = @ScoreInTheory,[ScoreInPratise] = @ScoreInPratise WHERE  [Id]=@Id";

		private static readonly string SqlDeleteStaffTrainDetail = "DELETE [StaffTrainDetail] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllStaffTrainDetail = "SELECT [Id], [TrainCode],[StaffCode],[ScoreInTheory],[ScoreInPratise] FROM [StaffTrainDetail]";
		#endregion

		#region IStaffTrainDetailProvider Members
		public Core.Business.StaffTrainDetail Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectStaffTrainDetail);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.StaffTrainDetail staffTrainDetail = new Core.Business.StaffTrainDetail();

				if (!reader.IsDBNull(0)) staffTrainDetail.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) staffTrainDetail.TrainCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) staffTrainDetail.StaffCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) staffTrainDetail.ScoreInTheory = reader.GetDecimal(3);
				if (!reader.IsDBNull(4)) staffTrainDetail.ScoreInPratise = reader.GetDecimal(4);

				return staffTrainDetail;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.StaffTrainDetail staffTrainDetail)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TrainCode", SqlDbType.NVarChar, staffTrainDetail.TrainCode);
			sql.AddParameter("@StaffCode", SqlDbType.NVarChar, staffTrainDetail.StaffCode);
			sql.AddParameter("@ScoreInTheory", SqlDbType.Decimal, staffTrainDetail.ScoreInTheory);
			sql.AddParameter("@ScoreInPratise", SqlDbType.Decimal, staffTrainDetail.ScoreInPratise);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertStaffTrainDetail);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) staffTrainDetail.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.StaffTrainDetail staffTrainDetail)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@TrainCode", SqlDbType.NVarChar, staffTrainDetail.TrainCode);
			sql.AddParameter("@StaffCode", SqlDbType.NVarChar, staffTrainDetail.StaffCode);
			sql.AddParameter("@ScoreInTheory", SqlDbType.Decimal, staffTrainDetail.ScoreInTheory);
			sql.AddParameter("@ScoreInPratise", SqlDbType.Decimal, staffTrainDetail.ScoreInPratise);
			sql.AddParameter("@Id", SqlDbType.Int, staffTrainDetail.Id);
			sql.ExecuteSql(SqlUpdateStaffTrainDetail);
		}

		public void Delete(Core.Business.StaffTrainDetail staffTrainDetail)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, staffTrainDetail.Id);
			sql.ExecuteSql(SqlDeleteStaffTrainDetail);
		}

		public IList<Core.Business.StaffTrainDetail> GetAllStaffTrainDetail()
		{
			IList<Core.Business.StaffTrainDetail> staffTrainDetaillist = new List<Core.Business.StaffTrainDetail>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllStaffTrainDetail);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.StaffTrainDetail staffTrainDetail = new Core.Business.StaffTrainDetail();

					if (!reader.IsDBNull(0)) staffTrainDetail.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) staffTrainDetail.TrainCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) staffTrainDetail.StaffCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) staffTrainDetail.ScoreInTheory = reader.GetDecimal(3);
					if (!reader.IsDBNull(4)) staffTrainDetail.ScoreInPratise = reader.GetDecimal(4);

					staffTrainDetail.MarkOld();
					staffTrainDetaillist.Add(staffTrainDetail);
				}
				reader.Close();
			}
			return staffTrainDetaillist;
		}
		#endregion
	}
}
